<?php
/*
 * ebiz front
 *
 * Customer Model
 *
 * @author allen
 *
 */
require_once '../app/core/models/BaseDao.php';
require_once '../app/core/db/DBUtil.class.php';

class UserDaoTest extends BaseDao
{
    private $tableName = "users";

    public function __construct($dbUtil){
        parent::__construct($dbUtil);
        $this->_name = $this->tableName;
    }
    
    public function saveUser($param){
    	//this is register  
    	if(count($param)==6){
    		$sql = "insert into $this->tableName (user_num, name, email, password, last_ip, campus_id, modified, created , status) values (?,?,?,?,?,?,now(),now(), 0 )";       
    	} else {
    	    $sql = "insert into $this->tableName (user_num, name, email, password, last_ip, modified, created , status) values (?,?,?,?,?,now(),now() ,0 )";
    	}
    	return $this->dbUtil->ExecuteQuery($sql, $param);
    }

    public function getByEmailAndPassword($param){     //this is login function
        $sql="select * from $this->tableName where lower(email)= ? and password = ? and ( status = 0 or status = 1 or status = 3 ) and is_delete = 0";
        return $this->dbUtil->SelectData($sql,$param);
    }

    public function getByName($name){    //validate Email uniqueness
    	$name = strtolower($name);
        $sql="select * from $this->tableName where lower(name) = ? and is_delete = 0 ";
        $param = array($name);
        return $this->dbUtil->SelectData($sql,$param);
    }
    public function getByEmail($email){     //validate Email uniqueness
    	$email = strtolower($email);
        $sql="select * from $this->tableName where lower(email) = ? and is_delete = 0 ";
        $param = array($email);
        return $this->dbUtil->SelectData($sql,$param);
    }

    public function getByEmailForPassword($email){ //this function is used in getPassword function of CustomerService to findback password
//        $sql="select * from $this->tableName where lower(email) = ? and is_delete = 0 and ( status = 1 or status = 3 )";
        $sql="select * from $this->tableName where lower(email) = ? and is_delete = 0 ";
        $param = array($email);
        return $this->dbUtil->SelectData($sql,$param);
    }

    public function updateActivateKey($param){  //generate the activate_key
    	$sql = "update $this->tableName set activate_key = ? where id = ? and is_delete=0";
    	return $this->dbUtil->ExecuteQuery($sql,$param);
    }
    
    /*
     * @authoer: xiaoyuemian
     * created at 2010-02-18 23:46
     */
	public function getByUserNum($userNum){     //validate Email uniqueness
        $sql="select * from $this->tableName where user_num = ? and is_delete = 0 ";
        $param = array($userNum);
        return $this->dbUtil->SelectData($sql,$param);
    }

    public function getActivateKey($activate_key){   //get the id if the user exist
    	$sql="select * from $this->tableName where activate_key=? and is_delete=0 and status=0";
    	$param = array($activate_key);
        return $this->dbUtil->SelectData($sql,$param);
    }

    public function getByActivateKey($activate_key){    //get the encode key and the register time in the 24 hours
        $sql="select * from $this->tableName where activate_key=? and is_delete=0 and modified > adddate(now(),interval -24 hour)";
        $param = array($activate_key);
        return $this->dbUtil->SelectData($sql,$param);
    }

    public function updateStatus($param){   //activation the status
        $sql = "update $this->tableName set status = ? , active_time = now() , activate_key = null where id = ? and is_delete=0";
        return $this->dbUtil->ExecuteQuery($sql,$param);
    }

    public function deleteActivateKey($id){  //when ststus==0 set the activate_key is null
    	$sql = "update $this->tableName set activate_key = null where id = ? and is_delete=0";
    	$param = array($id);
        return $this->dbUtil->ExecuteQuery($sql,$param);
    }

    public function updatePasswordKeyCreated($id){
        $sql = "update $this->tableName set password_key_created=now() where id=? and is_delete=0";
        $param = array($id);
        return $this->dbUtil->ExecuteQuery($sql, $param);
    }

    public function updateModifiedTime($id){
        $sql = "update $this->tableName set modified=now() where id=? and is_delete=0";
        $param = array($id);
        return $this->dbUtil->ExecuteQuery($sql, $param);
    }
    
    public function updateGetPasswordKey($result){
    	$param = array($result[0], $result[1]);
    	$sql = "update $this->tableName set get_password_key = ? where id = ? and is_delete=0";
    	return $this->dbUtil->ExecuteQuery($sql, $param);

    }

    public function updatePassword($param){     //update the new password
//      $sql = "update $this->tableName set password = ? where id=? and get_password_key = ? and is_delete = 0 and status = 1";
      $sql = "update $this->tableName set password = ? where id=? and get_password_key = ? and is_delete = 0 ";//if user not active he can update password
      return $this->dbUtil->ExecuteQuery($sql,$param);
    }
    
    public function getByGetPasswordKey($getPasswordKey){     //declare if id is right, and the findback time in the 24 hours
    	$sql="select * from $this->tableName where get_password_key=? and is_delete=0  and password_key_created > adddate(now(),interval -24 hour)";
        $param = array($getPasswordKey);
        return $this->dbUtil->SelectData($sql, $param);
    }

    public function deleteGetPasswordKey($id){
    	$sql = "update $this->tableName set get_password_key = null where id = ? and is_delete=0";
        $param = array($id);
        return $this->dbUtil->ExecuteQuery($sql,$param);
    }

    public function deleteUserById($id){    //delete user
        $sql = "update $this->tableName set is_delete = 1 where id = ?";
        $param = array($id);
        return $this->dbUtil->ExecuteQuery($sql,$param);
    }

	public function updateLastLoginById($id){     //update the new password
        $param = array($id);
		$sql = "update $this->tableName set last_login = now() where id=? and is_delete = 0 and ( status = 0 or status = 1 or status = 3 )";
        return $this->dbUtil->ExecuteQuery($sql,$param);
    }

    public function getCustomerById($id){   //get the customer info by id
        $sql="select * from $this->tableName where id=? and is_delete=0 and ( status = 0 or status = 1 or status = 3 )";
        $param = array($id);
        return $this->dbUtil->SelectData($sql, $param);
    }

    public function getUserById($id){
        $sql="select * from $this->tableName where id=? and is_delete=0 ";
        $param = array($id);
        return $this->dbUtil->SelectData($sql, $param);    	
    }
    
    public function getCustomerByCampusesId($campusesId){   //get the customer info by campuses id
        $param = array($campusesId);
//        $sql="select * from $this->tableName where campus_id = ? and is_delete = 0 and  status = 1 and certf = 3 ";
        $sql="select * from $this->tableName where campus_id = ? and is_delete = 0 and certf = 3 ";
        return $this->dbUtil->SelectData($sql, $param);
    }

    public function getCustomerCampusesNameByCid($customerId){
    	$param = array($customerId);
    	$sql = "select agent_campuses.area_name from $this->tableName
				join agent_campuses on users.campus_id = agent_campuses.id
				where users.id = ? and  users.is_delete = 0  ";
//				where users.id = ? and  users.is_delete = 0 and  ( users.`status` = 1 or users.`status` = 3 )  ";
    	return $this->dbUtil->SelectData($sql, $param);
    }

/*
* @author li.junfu
* */
    public function updateInfomaiton($param){    //update  customer   email and  fullname
//    	$sql = "update $this->tableName set name=? ,email=?,modified = now() where id=? and is_delete = 0 and ( status = 1 or status = 3 )";
    	$sql = "update $this->tableName set name=? ,email=?,modified = now() where id=? and is_delete = 0";
    	return $this->dbUtil->ExecuteQuery($sql,$param);
    }

	public function updatePwd($param){          //update   customer  password
//		$sql="update $this->tableName set password = ?,modified = now() where id=? and is_delete=0 and ( status = 1 or status = 3 )";
		$sql="update $this->tableName set password = ?,modified = now() where id=? and is_delete=0";
		return $this->dbUtil->ExecuteQuery($sql,$param);
	}

/*
 * @author steve
 */
   public function updateAddress($param){
//   	   $sql="update $this->tableName set team_name=?,full_name=?,district=?,city=?,addr=?,postal_code=?,mobile=?,phone=?,modified = now() where id=? and is_delete=0 and ( status = 1 or status = 3 )";
   	   $sql="update $this->tableName set team_name=?,full_name=?,district=?,city=?,addr=?,postal_code=?,mobile=?,phone=?,modified = now() where id=? and is_delete=0 ";
	   return $this->dbUtil->ExecuteQuery($sql,$param);
   }
   
   /*
    * @author xiaoyuemian
    */
   	public function updateUserNumById($userNum, $uId){
   		$param = array($userNum, $uId);
		$sql="update $this->tableName set user_num = ?, modified = now() where id=? and is_delete=0 and ( status = 1 or status = 3 )";
		return $this->dbUtil->ExecuteQuery($sql,$param);
    }   
}




